package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import model.Database;
import model.DatePicker;
import model.EmpTableModel;
import model.Employee;
import model.FormatFactory;
import model.IOFile;
import model.NumberRenderer;
import model.StoreImages;
import model.UICommand;

import org.jdesktop.swingx.JXCollapsiblePane;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXSearchField;
import org.jdesktop.swingx.JXSearchField.LayoutStyle;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTextField;

import com.alee.laf.WebLookAndFeel;
import com.alee.laf.toolbar.WebToolBar;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controller.EmployeeController;

public class EmployeePanel extends JPanel {
	private static final long serialVersionUID = -8736311694292530583L;
	private JTextField txt_id;
	private JTextField txt_name;
	private JTextField txt_ident;
	private JTextField txt_phone;
	private JTextField txt_address;
	private JXDatePicker dPicker_birthday;
	private JXDatePicker dPicker_dateBegin;
	private JComboBox<String> cbx_regency;
	private JXTable table;
	private JXTaskPane taskPane;
	private JComboBox<String> comboBox;
	private JPanel panelFunc;
	private JButton btnThm;
	private EmpTableModel tableModel;
	private JButton btnAdd;
	private JButton btnDel;
	private JButton btnEdit;
	private JButton btnReload;
	private JFormattedTextField editor_birthday;
	private JFormattedTextField editor_dateBegin;
	private JMenuItem mntmThm;
	private JMenuItem mntmXa;
	private JMenuItem mntmSa;
	private JMenuItem mntmTiLi;
	private JPanel panel_2;
	private JPanel panel_3;
	private JLabel lblSearch;
	private JXSearchField searchField;
	private JLabel label;
	private JButton btnSearch;
	private JXCollapsiblePane collapsiblePane;
	private JCheckBox lblNewLabel_7;
	private JCheckBox lblNewLabel_9;
	private JCheckBox lblNewLabel_10;
	private JXTextField txtName;
	private JXTextField txtPhone;
	private JXTextField txtID;
	private JCheckBox lblNewLabel_11;
	private JComboBox<String> cbxRegency;
	private JComboBox<String> cbxSex;
	private JXTextField txtIdentify;
	private JCheckBox chckbxNewCheckBox;
	private JCheckBox chckbxNewCheckBox_1;
	private JCheckBox chckbxNewCheckBox_2;
	private JCheckBox chckbxNewCheckBox_3;
	private JXDatePicker datePicker;
	private JXDatePicker dPicker_date;
	private JCheckBox chckbxTngHin;
	private HashMap<String, JComponent> listTextField;

	public EmployeePanel(EmpTableModel tableModel, EmployeeController control) {
		this.tableModel = tableModel;
		this.listTextField = new HashMap<String, JComponent>();
		setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		dPicker_birthday = DatePicker.createDatePicker(
				DatePicker.DEFAULT_WIDTH_COLUMN, null);
		editor_birthday = dPicker_birthday.getEditor();
		dPicker_birthday.setFormats("yyyy-MM-dd");
		dPicker_birthday.setEditable(false);

		dPicker_dateBegin = DatePicker.createDatePicker(
				DatePicker.DEFAULT_WIDTH_COLUMN, null);
		editor_dateBegin = dPicker_dateBegin.getEditor();
		dPicker_dateBegin.setFormats("yyyy-MM-dd");
		dPicker_dateBegin.setEditable(false);

		JPanel tablePanel = new JPanel();
		panel_1.add(tablePanel, BorderLayout.CENTER);
		tablePanel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"Danh s\u00E1ch nh\u00E2n vi\u00EAn", TitledBorder.LEADING,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		tablePanel.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		tablePanel.add(scrollPane, BorderLayout.CENTER);

		// TODO table
		table = new JXTable(tableModel);
		table.setHorizontalScrollEnabled(true);
		scrollPane.setViewportView(table);

		// alignment center
		FormatFactory.formatHeader(table, SwingConstants.CENTER);
		FormatFactory.formatHighlighter(table, null);

		table.addMouseListener(control);

		// renderer
		table.getColumnModel().getColumn(0)
				.setCellRenderer(new NumberRenderer());
		table.getColumnModel().getColumn(0).setPreferredWidth(90);
		table.getColumnModel().getColumn(0).setMaxWidth(90);

		// TODO tool bar
		WebToolBar toolBar = new WebToolBar();
		FormatFactory.formatToolBar(toolBar);
		tablePanel.add(toolBar, BorderLayout.EAST);

		btnAdd = FormatFactory.createButtonToolBar("Thêm", UICommand.SHOW_CMD,
				StoreImages.getIcon(StoreImages.NEW_ICON), control);
		toolBar.add(btnAdd);

		btnEdit = FormatFactory.createButtonToolBar("Sửa",
				UICommand.MODIFY_CMD,
				StoreImages.getIcon(StoreImages.EDIT_ICON), control);
		toolBar.add(btnEdit);

		btnDel = FormatFactory.createButtonToolBar("Xóa", UICommand.DELETE_CMD,
				StoreImages.getIcon(StoreImages.DELETE_ICON), control);
		toolBar.add(btnDel);

		btnReload = FormatFactory.createButtonToolBar("Tải lại dữ liệu",
				UICommand.RELOAD_CMD,
				StoreImages.getIcon(StoreImages.REFRESH_ICON), control);
		toolBar.add(btnReload);

		toolBar.addToEnd(FormatFactory.createButtonToolBar("Cài đặt",
				UICommand.SETTINGS_CMD,
				StoreImages.getIcon(StoreImages.SETTINGS_ICON), control));

		// TODO create popup menu
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		mntmThm = new JMenuItem("Thêm nhân viên");
		mntmThm.setActionCommand(UICommand.SHOW_CMD);
		mntmThm.addActionListener(control);
		popupMenu.add(mntmThm);

		mntmSa = new JMenuItem("Sửa thông tin");
		mntmSa.setActionCommand(UICommand.MODIFY_CMD);
		mntmSa.addActionListener(control);
		popupMenu.add(mntmSa);

		mntmXa = new JMenuItem("Xóa nhân viên");
		mntmXa.setActionCommand(UICommand.DELETE_CMD);
		mntmXa.addActionListener(control);
		popupMenu.add(mntmXa);
		popupMenu.addSeparator();

		mntmTiLi = new JMenuItem("Tải lại dữ liệu");
		mntmTiLi.setActionCommand(UICommand.RELOAD_CMD);
		mntmTiLi.addActionListener(control);
		popupMenu.add(mntmTiLi);

		panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.NORTH);
		panel_2.setLayout(new BorderLayout(0, 0));

		panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null,
				"Th\u00F4ng tin t\u00ECm ki\u1EBFm", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		panel_2.add(panel_3, BorderLayout.NORTH);
		panel_3.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("right:default:grow"), },
				new RowSpec[] { FormFactory.DEFAULT_ROWSPEC, }));

		lblSearch = new JLabel("Tìm kiếm");
		panel_3.add(lblSearch, "1, 1, right, default");

		searchField = new JXSearchField();
		searchField.setPrompt("Nhập tên nhân viên cần tìm");
		searchField.setToolTipText("Mặc định tìm kiếm theo tên nhân viên");
		searchField.setColumns(27);
		searchField.setLayoutStyle(LayoutStyle.MAC);
		panel_3.add(searchField, "3, 1, fill, default");

		label = new JLabel("");
		label.setToolTipText("Tìm kiếm nâng cao");
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				collapsiblePane.setCollapsed(!collapsiblePane.isCollapsed());
			}
		});
		label.setIcon(new ImageIcon(EmployeePanel.class
				.getResource("/images/advan.png")));
		panel_3.add(label, "5, 1");

		btnSearch = new JButton("");
		btnSearch.setFocusable(false);
		btnSearch.setIcon(new ImageIcon(EmployeePanel.class
				.getResource("/images/find.png")));
		panel_3.add(btnSearch, "7, 1");

		chckbxTngHin = new JCheckBox("Tự động hiện chi tiết nhân viên");
		chckbxTngHin.setSelected(true);
		panel_3.add(chckbxTngHin, "9, 1");

		collapsiblePane = new JXCollapsiblePane();
		collapsiblePane.setCollapsed(true);
		collapsiblePane.setBorder(new TitledBorder(null,
				"T\u00ECm ki\u1EBFm n\u00E2ng cao", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		panel_2.add(collapsiblePane, BorderLayout.CENTER);
		collapsiblePane.getContentPane().setLayout(
				new FormLayout(new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"), }, new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		lblNewLabel_7 = new JCheckBox("Mã nhân viên");
		lblNewLabel_7.setName("id");
		lblNewLabel_7.setActionCommand("test");
		lblNewLabel_7.addActionListener(control);
		collapsiblePane.getContentPane().add(lblNewLabel_7,
				"1, 1, left, default");

		txtID = new JXTextField();
		listTextField.put("id", txtID);
		txtID.setEnabled(false);
		collapsiblePane.getContentPane().add(txtID, "3, 1, fill, default");

		lblNewLabel_9 = new JCheckBox("Điện thoại");
		lblNewLabel_9.setName("phone");
		lblNewLabel_9.setActionCommand("test");
		lblNewLabel_9.addActionListener(control);
		collapsiblePane.getContentPane().add(lblNewLabel_9,
				"5, 1, left, default");

		txtPhone = new JXTextField();
		listTextField.put("phone", txtPhone);
		txtPhone.setEnabled(false);
		collapsiblePane.getContentPane().add(txtPhone, "7, 1, fill, default");

		chckbxNewCheckBox = new JCheckBox("Tên nhân viên");
		chckbxNewCheckBox.setName("name");
		chckbxNewCheckBox.setActionCommand("test");
		chckbxNewCheckBox.addActionListener(control);
		collapsiblePane.getContentPane().add(chckbxNewCheckBox,
				"1, 3, left, default");

		txtName = new JXTextField();
		listTextField.put("name", txtName);
		txtName.setEnabled(false);
		collapsiblePane.getContentPane().add(txtName, "3, 3, fill, default");

		chckbxNewCheckBox_1 = new JCheckBox("Ngày sinh");
		chckbxNewCheckBox_1.setName("birthday");
		chckbxNewCheckBox_1.setActionCommand("test");
		chckbxNewCheckBox_1.addActionListener(control);
		collapsiblePane.getContentPane().add(chckbxNewCheckBox_1,
				"9, 1, left, default");

		datePicker = new JXDatePicker();
		listTextField.put("birthday", datePicker);
		datePicker.setEnabled(false);
		collapsiblePane.getContentPane().add(datePicker, "11, 1");

		lblNewLabel_11 = new JCheckBox("Chức vụ");
		lblNewLabel_11.setName("regency");
		lblNewLabel_11.setActionCommand("test");
		lblNewLabel_11.addActionListener(control);
		collapsiblePane.getContentPane().add(lblNewLabel_11,
				"5, 3, left, default");

		cbxRegency = new JComboBox<String>();
		listTextField.put("regency", cbxRegency);
		cbxRegency.setEnabled(false);
		collapsiblePane.getContentPane().add(cbxRegency, "7, 3, fill, default");

		chckbxNewCheckBox_3 = new JCheckBox("Ngày làm");
		chckbxNewCheckBox_3.setName("bg_date");
		chckbxNewCheckBox_3.setActionCommand("test");
		chckbxNewCheckBox_3.addActionListener(control);
		collapsiblePane.getContentPane().add(chckbxNewCheckBox_3,
				"9, 3, left, default");

		dPicker_date = new JXDatePicker();
		listTextField.put("bg_date", dPicker_date);
		dPicker_date.setEnabled(false);
		collapsiblePane.getContentPane().add(dPicker_date, "11, 3");

		lblNewLabel_10 = new JCheckBox("Chứng minh thư");
		lblNewLabel_10.setName("iden");
		lblNewLabel_10.setActionCommand("test");
		lblNewLabel_10.addActionListener(control);
		collapsiblePane.getContentPane().add(lblNewLabel_10,
				"1, 5, left, default");

		txtIdentify = new JXTextField();
		listTextField.put("iden", txtIdentify);
		txtIdentify.setEnabled(false);
		collapsiblePane.getContentPane()
				.add(txtIdentify, "3, 5, fill, default");

		chckbxNewCheckBox_2 = new JCheckBox("Giới tính");
		chckbxNewCheckBox_2.setName("sex");
		chckbxNewCheckBox_2.setActionCommand("test");
		chckbxNewCheckBox_2.addActionListener(control);
		collapsiblePane.getContentPane().add(chckbxNewCheckBox_2,
				"5, 5, left, default");

		cbxSex = new JComboBox<String>();
		cbxSex.setModel(new DefaultComboBoxModel<String>(new String[] { "Nam",
				"Nữ" }));
		listTextField.put("sex", cbxSex);
		cbxSex.setEnabled(false);
		collapsiblePane.getContentPane().add(cbxSex, "7, 5, fill, default");

		taskPane = new JXTaskPane();
		panel_2.add(taskPane, BorderLayout.SOUTH);
		taskPane.setCollapsed(true);
		taskPane.setFocusable(false);
		taskPane.getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		taskPane.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"Th\u00F4ng tin nh\u00E2n vi\u00EAn", TitledBorder.LEADING,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"), }));

		JLabel lblNewLabel = new JLabel("Tên NV");
		panel.add(lblNewLabel, "1, 1, left, default");

		// TODO
		txt_name = new JTextField();
		txt_name.setBackground(Color.WHITE);
		txt_name.setEditable(false);
		panel.add(txt_name, "3, 1, fill, default");
		txt_name.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("Mã NV");
		panel.add(lblNewLabel_1, "5, 1, left, default");

		txt_id = new JTextField();
		txt_id.setEditable(false);
		panel.add(txt_id, "7, 1, fill, default");
		txt_id.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Ngày sinh");
		panel.add(lblNewLabel_2, "1, 3, left, default");
		panel.add(dPicker_birthday, "3, 3, fill, default");

		JLabel lblNewLabel_6 = new JLabel("Số CMND");
		panel.add(lblNewLabel_6, "5, 3, left, default");

		txt_ident = new JTextField();
		txt_ident.setBackground(Color.WHITE);
		txt_ident.setEditable(false);
		panel.add(txt_ident, "7, 3, fill, default");
		txt_ident.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("Giới tính");
		panel.add(lblNewLabel_5, "1, 5, left, default");

		comboBox = new JComboBox<String>();
		comboBox.setEnabled(false);
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {
				"Nam", "Nữ" }));
		panel.add(comboBox, "3, 5, fill, default");

		JLabel lblNewLabel_3 = new JLabel("Ngày làm");
		panel.add(lblNewLabel_3, "5, 5, left, default");
		panel.add(dPicker_dateBegin, "7, 5, fill, default");

		JLabel lblNewLabel_8 = new JLabel("Điện thoại");
		panel.add(lblNewLabel_8, "1, 7, left, default");

		txt_phone = new JTextField();
		txt_phone.setBackground(Color.WHITE);
		txt_phone.setEditable(false);
		panel.add(txt_phone, "3, 7, fill, default");
		txt_phone.setColumns(10);

		JLabel lblChcV = new JLabel("Chức vụ");
		panel.add(lblChcV, "5, 7, left, default");

		cbx_regency = new JComboBox<String>();
		cbx_regency.setBackground(Color.WHITE);
		cbx_regency.setEnabled(false);
		panel.add(cbx_regency, "7, 7, fill, default");

		JLabel lblNewLabel_4 = new JLabel("Địa chỉ");
		panel.add(lblNewLabel_4, "1, 9, left, default");

		txt_address = new JTextField();
		txt_address.setBackground(Color.WHITE);
		txt_address.setEditable(false);
		panel.add(txt_address, "3, 9, 5, 1, fill, default");
		txt_address.setColumns(10);

		// TODO function panel
		panelFunc = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
		panelFunc.setVisible(false);
		panel.add(panelFunc, "1, 11, 7, 1, fill, fill");

		btnThm = new JButton("Thêm");
		btnThm.setActionCommand(UICommand.ADD_CMD);
		btnThm.addActionListener(control);
		panelFunc.add(btnThm);

		JButton btnHy = new JButton("Hủy");
		btnHy.setActionCommand(UICommand.CANCEL_CMD);
		btnHy.addActionListener(control);
		btnHy.setPreferredSize(btnThm.getPreferredSize());
		panelFunc.add(btnHy);
		taskPane.setScrollOnExpand(true);

		loadDataRegecy();
	}

	// TODO set editable
	public void editable(boolean b) {
		txt_address.setEditable(b);
		dPicker_birthday.setEditable(b);
		dPicker_dateBegin.setEditable(b);
		txt_id.setEditable(false);
		txt_ident.setEditable(b);
		txt_name.setEditable(b);
		txt_phone.setEditable(b);
		cbx_regency.setEnabled(b);
		comboBox.setEnabled(b);
	}

	public void editTxtId(boolean b) {
		this.txt_id.setEnabled(b);
	}

	public void setTextButtonAdd(String str) {
		this.btnThm.setText(str);
	}

	public String getTextButtonAdd() {
		return btnThm.getText();
	}

	public int[] getSelectedRealRows() {
		int[] result = table.getSelectedRows();
		try {
			for (int i = 0; i < result.length; i++) {
				result[i] = table.convertRowIndexToModel(result[i]);
			}
		} catch (IndexOutOfBoundsException e) {
			e.printStackTrace(IOFile.getInstance());
		}
		return result;
	}

	// TODO show employee
	public void showDetail() {
		int[] selectRow = getSelectedRealRows();
		if (selectRow.length != 0) {
			Employee e = (Employee) tableModel.getRow(selectRow[0]);
			txt_name.setText(e.getFirstName().concat(" ")
					.concat(e.getLastName()));
			txt_id.setText(e.getId() + "");
			editor_birthday.setText(e.getBirthday().toString());
			txt_ident.setText(e.getIdent());
			txt_address.setText(e.getAddress());
			txt_phone.setText(e.getPhone());
			editor_dateBegin.setText(e.getDateBegin().toString());
			cbx_regency.setSelectedItem(e.getRegency());
			if (e.getSex().equalsIgnoreCase("nữ"))
				comboBox.setSelectedIndex(1);
			else
				comboBox.setSelectedIndex(0);
			if (chckbxTngHin.isSelected())
				taskPane.setCollapsed(false);
		}
	}

	public void setText(String idEmpl) {
		txt_name.setText("");
		txt_id.setText(idEmpl);
		editor_birthday.setText("");
		txt_ident.setText("");
		txt_address.setText("");
		txt_phone.setText("");
		editor_dateBegin.setText("");
		cbx_regency.setSelectedIndex(-1);
		comboBox.setSelectedIndex(0);
	}

	public void enableTable(boolean b) {
		table.setEnabled(b);
		table.clearSelection();
	}

	public void showPanelFunc(boolean b) {
		this.panelFunc.setVisible(b);
		enableToolBar(!b);
	}

	public void cancel() {
		if (getTextButtonAdd().equalsIgnoreCase("thêm")) {
			taskPane.setCollapsed(true);
			setText("");
		} else
			showDetail();
		showPanelFunc(false);
		enableTable(true);
		editable(false);
		enableToolBar(true);
	}

	public boolean checkNameIsNull() {
		return (txt_name.getText().equals("")) ? true : false;
	}

	public String[] getInfor() {
		String[] tmp = new String[10];
		tmp[0] = txt_id.getText(); // id
		String fullName = txt_name.getText();
		int offset = fullName.lastIndexOf(' ');
		if (offset != -1) {
			tmp[1] = fullName.substring(0, offset);// first name
			tmp[2] = fullName.substring(offset + 1); // last name
		} else {
			tmp[1] = fullName;
			tmp[2] = "";
		}
		tmp[3] = editor_birthday.getText();
		tmp[4] = comboBox.getSelectedItem().toString();
		tmp[5] = txt_ident.getText();
		tmp[6] = txt_address.getText();
		tmp[7] = txt_phone.getText();
		tmp[8] = editor_dateBegin.getText();
		tmp[9] = (String) cbx_regency.getSelectedItem();
		return tmp;
	}

	public void clearSelectionTable() {
		this.table.clearSelection();
	}

	public void enableToolBar(boolean b) {
		this.btnAdd.setEnabled(b);
		this.btnDel.setEnabled(b);
		this.btnEdit.setEnabled(b);
		this.btnReload.setEnabled(b);

		this.mntmThm.setEnabled(b);
		this.mntmSa.setEnabled(b);
		this.mntmXa.setEnabled(b);
		this.mntmTiLi.setEnabled(b);
	}

	public void setCollapsedTaskpane(boolean b) {
		this.taskPane.setCollapsed(b);
	}

	private void loadDataRegecy() {
		Connection connect = null;
		try {
			connect = Database.connect();
			PreparedStatement pre = connect
					.prepareStatement("SELECT name FROM regency");
			ResultSet rs = pre.executeQuery();

			while (rs.next()) {
				this.cbx_regency.addItem(rs.getString(1));
				this.cbxRegency.addItem(rs.getString(1));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

	public static void main(String[] args) {
		WebLookAndFeel.install();
		JFrame frame = new JFrame();
		EmpTableModel tableModel = new EmpTableModel();
		EmployeeController control = new EmployeeController(tableModel);
		frame.getContentPane().add(control.getView());
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
	}

	public void enableTextField(String name, boolean b) {
		listTextField.get(name).setEnabled(b);
	}

}
